import * as THREE from "three";

const FlyLineShader1 = {
  shaderID: 'FlyLineShader1',

  uniforms: {
    'iTime': { value: 1.0 },
    // 分别保存：整个线段的长度；流光长度；多线段流光1，单线段流光0；背景轨迹的透明度
    'wholeAndFlowSize': { value: new THREE.Vector4(100, 10, 1, 1) },
    'flowColor': { value: new THREE.Color('#55bb5f') },
    'trackColor': { value: new THREE.Color('#0d150e') }
  },

  vertexShader: /* glsl */`
  attribute float t;
  varying float tVal;
  
  void main() {
    tVal = t;
    gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
  }`,

  fragmentShader: /* glsl */`
	uniform vec3 flowColor;
	uniform vec3 trackColor;
	uniform vec4 wholeAndFlowSize;
  uniform float iTime;
  varying float tVal;
 
  void main() {
    float whole = wholeAndFlowSize.x;
    float period = wholeAndFlowSize.y;
    float xVal = mod(tVal - iTime, whole);
    float b;
    if (wholeAndFlowSize.z > 0.0) {
      b = smoothstep(0.01, period - 0.01, mod(xVal, period));
    } else {
      b = smoothstep(period, period * 2.0, xVal);
    }
    
    vec4 colorFlow = vec4(flowColor, 1.0);
    vec4 colorWhite = vec4(vec3(1.0 - wholeAndFlowSize.w), 0.0);
    float k = abs(0.5 - b) * 2.0;
    vec4 color = mix(colorFlow, colorWhite, k) + k * wholeAndFlowSize.w * vec4(trackColor, 1.0);
    gl_FragColor = color;
  }
`
}

export default FlyLineShader1;
